﻿using System.Text;
using StockSharp.BusinessEntities;

namespace StockSharp.Connectors.Alfa
{
	using StockSharp.AlfaDirect;

	public static class PrintUtils
	{
		public static string Log(Security sec)
		{
			var sb = new StringBuilder();
			sb.AppendLine("<------- " + sec.Name + " ------->");
			sb.AppendLine("Id: " + sec.Id);
			sb.AppendLine("ShortName: " + sec.ShortName);
			sb.AppendLine("ExpiryDate: " + sec.ExpiryDate);
			sb.AppendLine("State: " + sec.State);
			sb.AppendLine("Code: " + sec.Code);
			sb.AppendLine("Exchange: " + sec.Exchange.Name);
			sb.AppendLine("MarginBuy: " + sec.MarginBuy);
			sb.AppendLine("MarginSell: " + sec.MarginSell);
			sb.AppendLine("MinStepSize: " + sec.MinStepSize);
			sb.AppendLine("MinStepPrice: " + sec.MinStepPrice);
			sb.AppendLine("Type: " + sec.Type);
			sb.AppendLine("OpenInteres: " + sec.GetOpenInteres());
			sb.AppendLine("OpenPrice: " + sec.OpenPrice);
			sb.AppendLine("<-------------------------------->");

			return sb.ToString();
		}

		public static string Log(Portfolio portfolio)
		{
			var sb = new StringBuilder();

			sb.AppendLine("<------- " + portfolio.Name + " ------->");
			sb.AppendLine("BeginAmount: " + portfolio.BeginAmount);
			sb.AppendLine("CurrentAmount: " + portfolio.CurrentAmount);
			sb.AppendLine("<-------------------------------->");

			return sb.ToString();
		}

		public static string Log(Trade trade)
		{
			var sb = new StringBuilder();

			sb.Append("Trade: ").Append(trade.Id).Append(", ").Append(trade.Time).Append(", ");
			sb.Append(trade.OrderDirection).Append(", ").Append(trade.Security).Append(", ");
			sb.Append(trade.Price).Append(", ").Append(trade.Volume);

			return sb.ToString();
		}

		public static string LogDetails(Security sec)
		{
			var sb = new StringBuilder();

			sb.AppendLine("<------- " + sec.Name + " ------->");
			sb.AppendLine("Id: " + sec.Id);
			sb.AppendLine("BestAsk: " + sec.BestAsk);
			sb.AppendLine("BestBid: " + sec.BestBid);
			sb.AppendLine("HighPrice: " + sec.HighPrice);
			sb.AppendLine("LowPrice: " + sec.LowPrice);
			sb.AppendLine("OpenInteres: " + sec.GetOpenInteres());
			if(sec.LastTrade != null)
			{
				sb.AppendLine("LastPrice: " + sec.LastTrade.OrderDirection + ", " + sec.LastTrade.Price + ", " + sec.LastTrade.Volume);	
			}
			sb.AppendLine("<-------------------------------->");

			return sb.ToString();
		}

		public static string Log(Position position)
		{
			var sb = new StringBuilder();
			sb.Append("Position: ").Append(position.Portfolio.Name);
			sb.Append(", ").Append(position.Security.Code);
			sb.Append(", ").Append(position.CurrentValue);

			return sb.ToString();
		}

		public static string Log(MyTrade myTrade)
		{
			var sb = new StringBuilder();
			sb.AppendLine("<----------- my trade  #" + myTrade.Order.Id + " ---------->");
			sb.AppendLine(Log(myTrade.Order));
			// TODO: print Trade
			sb.AppendLine("<----------- end my trade ---------->");

			return sb.ToString();
		}

		public static string Log(Order order)
		{
			var sb = new StringBuilder();

			sb.Append("<----------- order #").Append(order.Id).AppendLine(" ---------->");
			sb.Append("Transac id: ").Append(order.TransactionId).AppendLine();
			sb.Append("Portfolio: ").AppendLine(order.Portfolio.Name);
			sb.Append("Direction: ").AppendLine(order.Direction.ToString());
			sb.Append("Price: ").Append(order.Price).AppendLine();
			sb.Append("Volume: ").Append(order.Volume).AppendLine();
			sb.Append("Time: ").Append(order.Time).AppendLine();
			sb.Append("Security: ").Append(order.Security.Code).AppendLine();
			sb.Append("State: ").AppendLine(order.State.ToString());

			if(order.CancelTime != null)
			{
				sb.Append("CancelTime: ").Append(order.CancelTime.Value).AppendLine();
			}

			sb.AppendLine("<----------- end order ---------->");

			return sb.ToString();
		}
	}
}
